package com.maolong.controller;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.maolong.pojo.fuction.result.PageResult;
import com.maolong.pojo.fuction.result.Result;
import com.maolong.util.AliOssUtil;
import com.maolong.pojo.dto.ProdDTO;
import com.maolong.pojo.dto.ProdSaveDTO;
import com.maolong.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.net.URL;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author maolong
 * @since 2025-02-09
 */

@Slf4j
@RestController
@RequestMapping("/Prod")
@Api(tags="商品模块")
public class ProductController {
    @Autowired
    IProductService productService;

    @Autowired
    AliOssUtil aliOssUtil;

    @ApiOperation("分页查询商品")
    @PostMapping("/list")
    public Result<PageResult> query(@RequestBody ProdDTO prodDTO){
        log.info("分页查询商品,参数:{}",prodDTO);
        PageResult query = productService.query(prodDTO);
        return Result.success(query);
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Integer ids){
        log.info("删除商品,参数:{}",ids);
        productService.delete(ids);
        return Result.success();
    }




    @ApiOperation("新增商品或保存商品")
    @PostMapping("/save")
    public Result save(@RequestBody ProdSaveDTO prodSaveDTO) throws Exception {
        log.info("新增商品,参数:{}",prodSaveDTO);
        productService.
                save(prodSaveDTO);
        return Result.success();

    }

    @ApiOperation("生成条形码")
    @GetMapping("/generate")
    public ResponseEntity<String> generate(@RequestParam String id,
                                           @RequestParam String code) throws WriterException {
        log.info("生成条形码,参数:{}{}",id,code);
        try {
            // 生成条形码内容
            String content = id + "+" + code;
            byte[] imageBytes = aliOssUtil.generateBarcode(content);

            // 上传到阿里云 OSS
            String objectName = id + "_" + code + ".png";
            String fileUrl = aliOssUtil.upload(imageBytes, objectName);
            // 返回存储的 URL
            return ResponseEntity.ok(fileUrl);
        } catch (Exception e) {
            return ResponseEntity.status(500).body("生成条形码并上传失败");
        }
    }


    @ApiOperation("解析条形码")
    @GetMapping("/parse")
    public  String parseBarcode(String imageUrl) throws Exception {
        // 下载图片
        URL url = new URL(imageUrl);
        InputStream inputStream = url.openStream();
        BufferedImage bufferedImage = ImageIO.read(inputStream);

        // 使用 ZXing 解码条形码
        LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        MultiFormatReader reader = new MultiFormatReader();

        com.google.zxing.Result result = reader.decode(bitmap);

        // 返回条形码的内容
        return result.getText();
    }


}
